{
 "cells": [
  {
   "cell_type": "code",
   "execution_count": 1,
   "id": "studied-interim",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres0\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m2\u001b[39m"
      ]
     },
     "execution_count": 1,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "1+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 2,
   "id": "acute-cambodia",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "println(1+1)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 3,
   "id": "armed-sphere",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mx\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m2\u001b[39m"
      ]
     },
     "execution_count": 3,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val x = 1 + 1\n",
    "println(x) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 4,
   "id": "infrared-portal",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mx\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m2\u001b[39m"
      ]
     },
     "execution_count": 4,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val x: Int = 1 + 1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 5,
   "id": "synthetic-multiple",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mx\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m3\u001b[39m"
      ]
     },
     "execution_count": 5,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "var x = 1 + 1\n",
    "x = 3 // This compiles because \"x\" is declared with the \"var\" keyword.\n",
    "println(x * x) // 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 6,
   "id": "floral-devices",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    }
   ],
   "source": [
    "println({\n",
    "  val x = 1 + 1\n",
    "  x + 1\n",
    "}) // 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 8,
   "id": "forbidden-application",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres7\u001b[39m: \u001b[32mInt\u001b[39m => \u001b[32mInt\u001b[39m = <function1>"
      ]
     },
     "execution_count": 8,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "(x:Int) => x+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 9,
   "id": "purple-aircraft",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36maddone\u001b[39m: \u001b[32mInt\u001b[39m => \u001b[32mInt\u001b[39m = <function1>"
      ]
     },
     "execution_count": 9,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val addone = (x:Int) => x+1"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 10,
   "id": "frequent-mattress",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "2\n"
     ]
    }
   ],
   "source": [
    "println(addone(1))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 11,
   "id": "departmental-channel",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36madd\u001b[39m: (\u001b[32mInt\u001b[39m, \u001b[32mInt\u001b[39m) => \u001b[32mInt\u001b[39m = <function2>"
      ]
     },
     "execution_count": 11,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val add = (x:Int,y:Int)=>x+y"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 12,
   "id": "stupid-links",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "6\n"
     ]
    }
   ],
   "source": [
    "println(add(3,3))"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 13,
   "id": "developed-publisher",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mnoparameters\u001b[39m: () => \u001b[32mInt\u001b[39m = <function0>"
      ]
     },
     "execution_count": 13,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val noparameters = ()=>42"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 15,
   "id": "private-thirty",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "42\n"
     ]
    }
   ],
   "source": [
    "println(noparameters())"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 16,
   "id": "listed-lawrence",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "3\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36madd\u001b[39m"
      ]
     },
     "execution_count": 16,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def add(x: Int, y: Int): Int = x + y\n",
    "println(add(1, 2)) // 3"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 17,
   "id": "elegant-wellington",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "9\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36maddThenMultiply\u001b[39m"
      ]
     },
     "execution_count": 17,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def addThenMultiply(x: Int, y: Int)(multiplier: Int): Int = (x + y) * multiplier\n",
    "println(addThenMultiply(1, 2)(3)) // 9"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 20,
   "id": "advance-microwave",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mname\u001b[39m"
      ]
     },
     "execution_count": 20,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def name:String = System.getProperty(\"user.name\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 21,
   "id": "handled-schema",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "hello L6BD610_Luo!\n"
     ]
    }
   ],
   "source": [
    "println(\"hello \"+name+\"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 22,
   "id": "vertical-roots",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mfunction\u001b[39m \u001b[36mgetSquareString\u001b[39m"
      ]
     },
     "execution_count": 22,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "def getSquareString(input: Double): String = {\n",
    "  val square = input * input\n",
    "  square.toString\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 23,
   "id": "timely-record",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mres22\u001b[39m: \u001b[32mString\u001b[39m = \u001b[32m\"9980.010000000002\"\u001b[39m"
      ]
     },
     "execution_count": 23,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "getSquareString(99.9)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 24,
   "id": "behavioral-breath",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mGreeter\u001b[39m"
      ]
     },
     "execution_count": 24,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "class Greeter(x:String,y:String){\n",
    "    def greet (name:String):Unit=println(x+name+y)\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 25,
   "id": "contained-struggle",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mgreeter\u001b[39m: \u001b[32mGreeter\u001b[39m = ammonite.$sess.cmd23$Helper$Greeter@18b473a1"
      ]
     },
     "execution_count": 25,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val greeter = new Greeter(\"Hello, \", \"!\")"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 26,
   "id": "stone-world",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Scala developer!\n"
     ]
    }
   ],
   "source": [
    "greeter.greet(\"Scala developer\") "
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 27,
   "id": "finnish-editing",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mPoint\u001b[39m"
      ]
     },
     "execution_count": 27,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "case class Point(x: Int, y: Int)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 28,
   "id": "solved-insight",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "\u001b[36mpoint\u001b[39m: \u001b[32mPoint\u001b[39m = \u001b[33mPoint\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\r\n",
       "\u001b[36manotherPoint\u001b[39m: \u001b[32mPoint\u001b[39m = \u001b[33mPoint\u001b[39m(\u001b[32m1\u001b[39m, \u001b[32m2\u001b[39m)\r\n",
       "\u001b[36myetAnotherPoint\u001b[39m: \u001b[32mPoint\u001b[39m = \u001b[33mPoint\u001b[39m(\u001b[32m2\u001b[39m, \u001b[32m2\u001b[39m)"
      ]
     },
     "execution_count": 28,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val point = Point(1, 2)\n",
    "val anotherPoint = Point(1, 2)\n",
    "val yetAnotherPoint = Point(2, 2)"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 29,
   "id": "smooth-weather",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Point(1,2) and Point(1,2) are the same.\n"
     ]
    }
   ],
   "source": [
    "if (point == anotherPoint){\n",
    "    println(point + \" and \" + anotherPoint + \" are the same.\")\n",
    "}else{\n",
    "    println(point + \" and \" + anotherPoint + \" are not the same.\")\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 30,
   "id": "sacred-annual",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Point(1,2) and Point(2,2) are different.\n"
     ]
    }
   ],
   "source": [
    "if (point == yetAnotherPoint) {\n",
    "  println(point + \" and \" + yetAnotherPoint + \" are the same.\")\n",
    "} else {\n",
    "  println(point + \" and \" + yetAnotherPoint + \" are different.\")\n",
    "} // Point(1,2) and Point(2,2) are different."
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 33,
   "id": "impaired-production",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mobject\u001b[39m \u001b[36mduixiang\u001b[39m"
      ]
     },
     "execution_count": 33,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "object duixiang {\n",
    "    private var counter = 0\n",
    "    def create():Int={\n",
    "        counter = counter + 1\n",
    "        counter\n",
    "    }\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 34,
   "id": "attended-montana",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "1\n",
      "2\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "\u001b[36mnewId\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m1\u001b[39m\r\n",
       "\u001b[36mnewerId\u001b[39m: \u001b[32mInt\u001b[39m = \u001b[32m2\u001b[39m"
      ]
     },
     "execution_count": 34,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "val newId: Int = duixiang.create()\n",
    "println(newId) // 1\n",
    "val newerId: Int = duixiang.create()\n",
    "println(newerId) // 2"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 35,
   "id": "accepted-living",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mGreeter\u001b[39m"
      ]
     },
     "execution_count": 35,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "trait Greeter {\n",
    "    def greet(name:String):Unit\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 36,
   "id": "raised-conversation",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mtrait\u001b[39m \u001b[36mGreeter\u001b[39m"
      ]
     },
     "execution_count": 36,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "trait Greeter {\n",
    "  def greet(name: String): Unit =\n",
    "    println(\"Hello, \" + name + \"!\")\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 37,
   "id": "packed-official",
   "metadata": {},
   "outputs": [
    {
     "name": "stdout",
     "output_type": "stream",
     "text": [
      "Hello, Scala developer!\n",
      "How are you, Scala developer?\n"
     ]
    },
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mclass\u001b[39m \u001b[36mDefaultGreeter\u001b[39m\r\n",
       "defined \u001b[32mclass\u001b[39m \u001b[36mCustomizableGreeter\u001b[39m\r\n",
       "\u001b[36mgreeter\u001b[39m: \u001b[32mDefaultGreeter\u001b[39m = ammonite.$sess.cmd36$Helper$DefaultGreeter@2ade769c\r\n",
       "\u001b[36mcustomGreeter\u001b[39m: \u001b[32mCustomizableGreeter\u001b[39m = ammonite.$sess.cmd36$Helper$CustomizableGreeter@7a9ab928"
      ]
     },
     "execution_count": 37,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "\n",
    "class DefaultGreeter extends Greeter\n",
    "\n",
    "class CustomizableGreeter(prefix: String, postfix: String) extends Greeter {\n",
    "  override def greet(name: String): Unit = {\n",
    "    println(prefix + name + postfix)\n",
    "  }\n",
    "}\n",
    "\n",
    "val greeter = new DefaultGreeter()\n",
    "greeter.greet(\"Scala developer\") // Hello, Scala developer!\n",
    "\n",
    "val customGreeter = new CustomizableGreeter(\"How are you, \", \"?\")\n",
    "customGreeter.greet(\"Scala developer\") // How are you, Scala developer?"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": 38,
   "id": "cheap-produce",
   "metadata": {},
   "outputs": [
    {
     "data": {
      "text/plain": [
       "defined \u001b[32mobject\u001b[39m \u001b[36mMain\u001b[39m"
      ]
     },
     "execution_count": 38,
     "metadata": {},
     "output_type": "execute_result"
    }
   ],
   "source": [
    "object Main {\n",
    "  def main(args: Array[String]): Unit =\n",
    "    println(\"Hello, Scala developer!\")\n",
    "}"
   ]
  },
  {
   "cell_type": "code",
   "execution_count": null,
   "id": "logical-pizza",
   "metadata": {},
   "outputs": [],
   "source": []
  }
 ],
 "metadata": {
  "kernelspec": {
   "display_name": "Scala",
   "language": "scala",
   "name": "scala"
  },
  "language_info": {
   "codemirror_mode": "text/x-scala",
   "file_extension": ".scala",
   "mimetype": "text/x-scala",
   "name": "scala",
   "nbconvert_exporter": "script",
   "version": "2.11.12"
  }
 },
 "nbformat": 4,
 "nbformat_minor": 5
}
